package com.demo.controller.aircraft;

import com.demo.common.model.Aircraft;
import com.demo.common.model.User;
import com.demo.service.AircraftService;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

//机型验证
public class AircraftValidator extends Validator {

    @Inject
    AircraftService aircraftService;

    protected void validate(Controller controller) {
        validateRequiredString("aircraft.airno", "airnoMsg", "请输入飞机编号!");
        validateRequiredString("aircraft.airtype", "airtypeMsg", "请输入飞机类型!");
        validateRequiredString("aircraft.seatnum", "seatnumMsg", "请输入座位数!");
        validateRequiredString("aircraft.location", "locationMsg", "请输入生产国或地区!");

        String id=controller.getPara("aircraft.id");
        String airNo=controller.getPara("aircraft.airno");
        if(aircraftService.findByAirNo(id,airNo)!=null){
            addError("airnoMsg", "飞机编号已被添加，请使用别的飞机编号！");
        }
    }

    protected void handleError(Controller controller) {
        controller.keepModel(Aircraft.class);

        String actionKey = getActionKey();
        if (actionKey.equals("/aircraft/save"))
            controller.render("add.html");
        else if (actionKey.equals("/aircraft/update"))
            controller.render("edit.html");
    }
}
